package com.java.controller;

import com.java.bean.*;
import com.java.mvc.ResponseBody;
import com.java.service.CourierService;
import com.java.service.UserService;
import com.java.util.DateFormatUtil;
import com.java.util.JSONUtil;
import sun.nio.cs.ext.MS874;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserController {

    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String,Integer>> data = UserService.console();
        Message msg = new Message();
        if(data.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/list.do")
    public String findAll(HttpServletRequest request, HttpServletResponse response){
        //1、获取起始值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2、获取当前页面数
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3、进行查询
        List<User> list = UserService.findAll(true, offset, pageNumber);
        List<BootStrapTableUser> list1 = new ArrayList<>();
        for (User u:list) {
            String regTime = DateFormatUtil.format(u.getRegTime());
            String preTime = u.getPreTime()==null?"--":DateFormatUtil.format(u.getPreTime());
            String status = u.getStatus()==0?"删除":"已删除";
            BootStrapTableUser u2 = new BootStrapTableUser(u.getId(),u.getUsername(),u.getUserPhone(),u.getPassword(),u.getUserIdNumber(),regTime,preTime,status);
            list1.add(u2);
        }
        List<Map<String, Integer>> console = UserService.console();
        Integer total = console.get(0).get("user_size");
        //4.    将集合封装为 bootstrap-table识别的格式
        ResultData<BootStrapTableUser> data = new ResultData<>();
        data.setRows(list1);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String userIdNumber = request.getParameter("userIdNumber");
        String password = request.getParameter("password");
        User u = new User();
        u.setUsername(username);
        u.setUserPhone(userPhone);
        u.setUserIdNumber(userIdNumber);
        u.setPassword(password);
        boolean flag = UserService.insert(u);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("用户录入成功！");
        }else {
            msg.setStatus(-1);
            msg.setResult("录入失败！");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        User u = UserService.findByUserPhone(userPhone);
        Message msg = new Message();
        if(u == null){
            msg.setStatus(-1);
            msg.setResult("未查找到该用户信息！");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功！");
            msg.setData(u);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));//id获取不到
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String userIdNumber = request.getParameter("userIdNumber");
        String password = request.getParameter("password");
        User u = new User();
        u.setUsername(username);
        u.setUserPhone(userPhone);
        u.setUserIdNumber(userIdNumber);
        u.setPassword(password);
        Message msg = new Message();
        boolean flag = UserService.update(id, u);
        if (flag){
            msg.setStatus(0);
            msg.setResult("修改成功!");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改失败!");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = UserService.delete(id);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
